/**
 *
 */
package com.googlecode.bip.entity;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 *
 * @author Claude CHOISNET
 */
public class UserValue
    implements Serializable, IUserValue, Comparable<UserValue>
{
    private static final long serialVersionUID = 2L;

    private int iduser; //key
    private int idquestion; //key
    private Timestamp userdate; //key
    private int uservalue;

    /* Optional - could be null */
    private String usercomment;

    /**
     * Create a NOT initialized UserValue
     */
    public UserValue()
    {
        //empty
    }

    /**
     * Create a shadow copy of giving UserValue
     *
     * @param o UserValue to clone
     */
    public UserValue( final UserValue o )
    {
        this.iduser     = o.iduser;
        this.idquestion = o.idquestion;
        if( o.userdate != null ) {
            this.userdate   = EntityHelper.getNormalizedTimestamp( o.userdate );
            }
        else {
            this.userdate   = null;
            }
        this.uservalue  = o.uservalue;
    }

    @Override
    public int compareTo( final UserValue o )
    {
        if( o == null ) {
            return Integer.MIN_VALUE;
            }
        if( this == o ) {
            return 0;
            }

        int cmp = this.iduser - o.iduser;
        if( cmp != 0 ) { return cmp; }

        cmp = this.idquestion - o.idquestion;
        if( cmp != 0 ) { return cmp; }

        cmp = TimestampComparator.compareTimestamp( this.userdate, o.userdate );
        if( cmp != 0 ) { return cmp; }

        cmp = this.uservalue - o.uservalue;
        if( cmp != 0 ) { return cmp; }

        cmp = StringComparator.compareString( this.usercomment, o.usercomment );
        return cmp;
    }

    /**
     *
     * @param rSet
     * @return
     * @throws SQLException
     */
    public IUserValue set( final ResultSet rSet ) throws SQLException
    {
        // from uservalues
        iduser      = rSet.getInt( "iduser" );
        idquestion  = rSet.getInt( "idquestion" );
        userdate    = rSet.getTimestamp( "userdate" );
        uservalue   = rSet.getInt( "uservalue" );

        // from uservaluecomments
        usercomment = rSet.getString( "usercomment" );

        return this;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#getIdUser()
     */
    @Override
    public int getIdUser()
    {
        return iduser;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#setIdUser(int)
     */
    @Override
    public void setIdUser( int iduser )
    {
        this.iduser = iduser;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#getIdQuestion()
     */
    @Override
    public int getIdQuestion()
    {
        return idquestion;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#setIdQuestion(int)
     */
    @Override
    public void setIdQuestion( int idquestion )
    {
        this.idquestion = idquestion;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#getUserDate()
     */
    @Override
    public Timestamp getUserDate()
    {
        return userdate;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#setUserDate(java.sql.Timestamp)
     */
    @Override
    public void setUserDate( final Timestamp userdate )
    {
        this.userdate = EntityHelper.getNormalizedTimestamp( userdate );
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#getUserValue()
     */
    @Override
    public int getUserValue()
    {
        return uservalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#setUserValue(int)
     */
    @Override
    public void setUserValue( int uservalue )
    {
        // TODO: check value from Question min/max
        this.uservalue = uservalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#getUserComment()
     */
    @Override
    public String getUserComment()
    {
        return usercomment;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IUserValue#setUserComment(java.lang.String)
     */
    @Override
    public void setUserComment( String usercomment )
    {
        if( usercomment != null ) {
            usercomment = usercomment.trim();
            }

        this.usercomment = usercomment;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();

        builder.append( "UserValue [iduser=" ).append( iduser )
               .append( ", idquestion=" ).append( idquestion )
               .append( ", userdate=" ).append( userdate )
               .append( ", uservalue=" ).append( uservalue )
               .append( ", usercomment=" ).append( usercomment )
               .append( "]" );

        return builder.toString();
    }
}
